package com.cht.commonUtil.infra;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.structure.I1S2;
import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class QuerySysList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( SP.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<I1S2> ises = new ArrayList<I1S2>();
		String sql = "SELECT sysSN,sysname,isLocalSys FROM envsystemlist";
		
		
		try {
			conn = Widgets.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while ( rs.next()) {
				I1S2 is = new I1S2();
				is.setI1( rs.getInt("sysSN") );
				is.setS1( rs.getString("sysname") );
				is.setS2( rs.getString("isLocalSys") );
				ises.add(is);
			}
		}  catch ( Exception e) {
			Widgets.errLog(e);
		}  finally {
			try {
				if ( rs != null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch (SQLException e) {
				System.out.println(Widgets.errProcess(e));
			}
		}

		response.getWriter().write(new Gson().toJson(ises));	
	}

}
